<?php
namespace app\index\controller;

use think\Container;
use think\facade\Cache;
use app\common\facade\Test;

class App extends Com
{
    //只为操作方法facade添加中间件方法check
    protected $middleware = [
        'Check'=>['only'=>['facade']]
    ];

    public function index()
    {
        Cache::store('redis')->set('redis_test','Redis_cash'.date('Y-m-d H:i:s'),3600);
        return 'Redis saved';
    }

    public function test(){
        $cache = app('cache');
        return $cache->store('redis')->get('redis_test');
        return Cache::store('redis')->get('redis_test');
    }

    //门面模式，静态代理
    public function facade(){
       return Test::info();
    }

    //容器和依赖注入测试
    public function classregist(){
        //在 /app/provider.php 注册类
        //return app('test')->index();
        //return app()->test->index();
        return Container::get('testApp')->index();
    }

    //路由设置
    public function route(){
        $name=input('name');
        return "The param come from Route('route/:name','app/route') is： ".$name.$this->request->name;
    }



    public function order(){
        $ordersn=input('ordersn','','trim');
        if(!$ordersn || 19!=strlen($ordersn)){
            $rtn['state']=0;
            return json($rtn);
        }

        $map['order_sn']=$ordersn;
        $map['state_pay']=1;
        $map['state']=1;        
        $res=db('isorder')->where($map)->field('id,order_sn ordersn,gname,attr')->find();
        if(!$res){
            $rtn['state']=0;
            return json($rtn);
        }
		
		if($res['state_create']){
            $rtn['state']=2;
            return json($rtn);			
		}
		
        $res['state']=1;
        return json($res);
    }

    //创建应用的回调
    public function create(){
        $ordersn=input('ordersn','','trim');
        $rtn['state']=0;
        if(!$ordersn || 19!=strlen($ordersn)){
            $rtn['msg']='缺少订单号';
            return json($rtn);
        }

        $aname=input('aname','','trim');
        $time_end=input('time_end');
        $loginkeys=input('loginkeys','','trim');
        $loginurl=input('loginurl','','trim');
        if(!$aname || !$time_end || !$loginurl || !$loginkeys){
            $rtn['msg']='缺少参数';
            return json($rtn);
        }

        $map['order_sn']=$ordersn;
        $map['state_pay']=1;
        $map['state']=1;
        $map['state_create']=0;
        $res=db('isorder')->where($map)->find();
        if(!$res){
            $rtn['msg']='订单不存在或已创建';
            return json($rtn);
        }

        $data['time_end']=$time_end>0?strtotime($time_end):-1;
        $data['time']=time();
        $data['uid']=$res['uid'];
        $data['oid']=$res['id'];
        $data['loginurl']=$loginurl;
        $data['loginkeys']=$loginkeys;
        $data['aname']=$aname;
        $aid=db('myapp')->insertGetId($data);
        if(!$aid){
            $rtn['msg']='应用创建失败';
            return json($rtn);
        }

        $udata['time_create']=time();
        $udata['state_create']=1;
        $udata['id_create']=$aid;
        $res_update=db('isorder')->where("id",$res['id'])->update($udata);
        if(!$res_update) {
            $rtn['msg']='应用创建失败';
            return json($rtn);
        }

        $rtn['state']=1;
        $rtn['msg']='应用创建成功';
        return json($rtn);
    }


}
